﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace clq_function
{
    class TypeConversion
    {
        /// <summary>
        /// 10进制转16进制
        /// clq
        /// 2012/12/19
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public string IntegerToHex(int num)
        {
            string hex = "";
            hex = Convert.ToString(num, 16);
            return hex;
        }

        /// <summary>
        /// 10进制转2进制
        /// clq
        /// 2012/12/19
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public string IntegerToBinary(int num)
        {
            string binary = "";
            binary = Convert.ToString(num, 2);
            return binary;
        }

        /// <summary>
        /// 2进制转10进制
        /// clq
        /// 2012/12/19
        /// </summary>
        /// <param name="binary"></param>
        /// <returns></returns>
        public int BinaryToInteger(string binary)
        {
            int num = 0;
            num = Convert.ToInt32(binary, 2);
            return num;
        }

        /// <summary>
        /// 2进制转16进制
        /// clq
        /// 2012/12/19
        /// </summary>
        /// <param name="binary"></param>
        /// <returns></returns>
        public string BinaryToHex(string binary)
        {
            string hex = "";
            hex = string.Format("{0:x}", Convert.ToInt32(binary, 2));
            return hex;
        }

        /// <summary>
        /// 16进制转10进制
        /// clq
        /// 2012/12/19
        /// </summary>
        /// <param name="hex"></param>
        /// <returns></returns>
        public int HexToInteger(string hex)
        {
            int num = 0;
            num = Convert.ToInt32(hex, 16);
            return num;
        }

        /// <summary>
        /// 16进制转2进制
        /// clq
        /// 2012/12/19
        /// </summary>
        /// <param name="hex"></param>
        /// <returns></returns>
        public string HexToBinary(string hex)
        {
            int num = 0;
            num = Convert.ToInt32(hex, 16);
            string binary = "";
            binary = Convert.ToString(num, 2);
            return binary;
        }

        /// <summary>
        /// 16进制转Ascii码
        /// clq
        /// 2012/12/19
        /// </summary>
        /// <param name="hex"></param>
        /// <returns></returns>
        public string HexToAscii(string hex)
        {
            byte[] buff = new byte[hex.Length / 2];
            int index = 0;
            for (int i = 0; i < hex.Length; i += 2)
            {
                buff[index] = Convert.ToByte(hex.Substring(i, 2), 16);
                ++index;
            }
            string result = Encoding.ASCII.GetString(buff);
            return result;
        }

        /// <summary>
        /// 16进制字符串转为16进制字符数组
        /// clq
        /// 2014/03/19
        /// </summary>
        /// <param name="newString"></param>
        /// <returns></returns>
        private byte[] Hex2ByteArr(string newString)
        {
            int len = newString.Length / 2;
            byte[] arr = new byte[len];
            for (int i = 0; i < len; i++)
            {
                arr[i] = Convert.ToByte(newString.Substring(i * 2, 2), 16);
            }
            return arr;
        }

        /// <summary> 
        /// 字符串转16进制字节数组 
        /// clq
        /// 2014/03/19
        /// </summary> 
        /// <param name="hexString"></param> 
        /// <returns></returns> 
        private byte[] strToToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }

        /// <summary> 
        /// 字节数组转16进制字符串 
        /// clq
        /// 2014/03/19
        /// </summary> 
        /// <param name="bytes"></param> 
        /// <returns></returns> 
        public string byteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                }
            }
            return returnStr;
        }

        /// <summary>
        /// Stream转byte[]
        /// 2014/05/07
        /// clq
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static byte[] StreamToBytes(Stream stream)
        {
            List<byte> bytes = new List<byte>();
            int temp = stream.ReadByte();
            while (temp != -1)
            {
                bytes.Add((byte)temp);
                temp = stream.ReadByte();
            }

            return bytes.ToArray();
        }
    }
}
